package com.wanxi.servlet.cuisine;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.CuisineService;
import com.wanxi.service.impl.CuisineServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.CuisineVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/cuisine/update")
public class CuisineUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String name = req.getParameter("name");
        String enable = req.getParameter("enable");
        String id = req.getParameter("id");
//        3.封装到model
        CuisineVO cuisineVO = CuisineVO.builder()
                .name(name)
                .id(Tool.strToInt(id))
                .enable(Tool.strToInt(enable))
                .build();
//        4.处理逻辑层
        CuisineService cuisineService = new CuisineServiceImpl();
        ResultDTO resultDTO = cuisineService.update(cuisineVO);
//        5.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
